package com.tony.vo;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;

/**
 * 
 */
public class MobileBaseBeanUtils {
	
		public static void copyFields(Object source, Object target) {
			
			Field[] sourceFields = source.getClass().getDeclaredFields();
			for (Field sourceField : sourceFields)
			{
				String sourceFiedName = sourceField.getName();
				try
				{
					sourceField.setAccessible(true);
					Object sourceFieldValue = sourceField.get(source);
					if (sourceFieldValue != null)
					{
						Field field = target.getClass().getDeclaredField(sourceFiedName);
						sourceFieldValue = getAdapterValue(sourceFieldValue, field);
						if (field != null && sourceFieldValue != null)
						{
							field.setAccessible(true);
							field.set(target, sourceFieldValue);
						}
					}
				}
				catch (Exception e)
				{
				}
			}
			
			
		}

		@SuppressWarnings("rawtypes")
		private static Object getAdapterValue(Object value, Field field) {
			Class fieldType = field.getType();
			Class objClass = value.getClass();
			if (objClass == fieldType) {
				return value;
			}
			if (fieldType == byte.class || fieldType == Byte.class) {
				if (objClass == String.class) {
					return Byte.parseByte((String) value);
				}
				if (objClass == Byte.class) {
					return ((Byte) value).byteValue();
				} else if (objClass == Short.class) {
					return ((Short) value).byteValue();
				} else if (objClass == Integer.class) {
					return ((Integer) value).byteValue();
				} else if (objClass == Long.class) {
					return ((Long) value).byteValue();
				} else if (objClass == Float.class) {
					return ((Float) value).byteValue();
				} else if (objClass == Double.class) {
					return ((Double) value).byteValue();
				}
			} else if (fieldType == short.class || fieldType == Short.class) {
				if (objClass == String.class) {
					return Short.parseShort((String) value);
				}
				if (objClass == Byte.class) {
					return ((Byte) value).shortValue();
				}
				if (objClass == Short.class) {
					return ((Short) value).shortValue();

				} else if (objClass == Integer.class) {
					return ((Integer) value).shortValue();

				} else if (objClass == Long.class) {
					return ((Long) value).shortValue();
				} else if (objClass == Float.class) {
					return ((Float) value).shortValue();
				} else if (objClass == Double.class) {
					return ((Double) value).shortValue();
				}
			} else if (fieldType == int.class || fieldType == Integer.class) {
				if (objClass == String.class) {
					return Integer.parseInt((String) value);
				}
				if (objClass == Byte.class) {
					return ((Byte) value).intValue();
				}
				if (objClass == Short.class) {
					return ((Short) value).intValue();

				} else if (objClass == Integer.class) {
					return ((Integer) value).intValue();

				} else if (objClass == Long.class) {
					return ((Long) value).intValue();
				} else if (objClass == Float.class) {
					return ((Float) value).intValue();
				} else if (objClass == Double.class) {
					return ((Double) value).intValue();
				}
			} else if (fieldType == long.class || fieldType == Long.class) {
				if (objClass == String.class) {
					return Long.parseLong((String) value);
				}
				if (objClass == Byte.class) {
					return ((Byte) value).longValue();
				}
				if (objClass == Short.class) {
					return ((Short) value).longValue();

				} else if (objClass == Integer.class) {
					return ((Integer) value).longValue();

				} else if (objClass == Long.class) {
					return ((Long) value).longValue();
				} else if (objClass == Float.class) {
					return ((Float) value).longValue();
				} else if (objClass == Double.class) {
					return ((Double) value).longValue();
				}
			} else if (fieldType == float.class || fieldType == Float.class) {
				if (objClass == String.class) {
					return Float.parseFloat((String) value);
				}
				if (objClass == Byte.class) {
					return ((Byte) value).floatValue();
				}
				if (objClass == Short.class) {
					return ((Short) value).floatValue();

				} else if (objClass == Integer.class) {
					return ((Integer) value).floatValue();

				} else if (objClass == Long.class) {
					return ((Long) value).floatValue();
				} else if (objClass == Float.class) {
					return ((Float) value).floatValue();
				} else if (objClass == Double.class) {
					return ((Double) value).floatValue();
				}
			} else if (fieldType == double.class || fieldType == Double.class) {
				if (objClass == String.class) {
					return Double.parseDouble((String) value);
				}
				if (objClass == Byte.class) {
					return ((Byte) value).doubleValue();
				}
				if (objClass == Short.class) {
					return ((Short) value).doubleValue();

				} else if (objClass == Integer.class) {
					return ((Integer) value).doubleValue();

				} else if (objClass == Long.class) {
					return ((Long) value).doubleValue();
				} else if (objClass == Float.class) {
					return ((Float) value).doubleValue();
				} else if (objClass == Double.class) {
					return ((Double) value).doubleValue();
				} else if (objClass == BigDecimal.class) {
					BigDecimal big = new BigDecimal(value.toString());
					return (big.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
				}
			} else if (fieldType == String.class) {
				return String.valueOf(value);
			} else if (fieldType == Date.class) {
				if (objClass == Timestamp.class) {
					return (Date) value;
				}
			}
			return null;
		}
}
